package org.ajc.auth.controller;

import org.ajc.auth.dto.RoleDTO;
import org.ajc.auth.service.IRoleService;
import org.ajc.auth.domain.Role;
import org.ajc.auth.query.RoleQuery;
import org.ajc.base.util.AjaxResult;
import org.ajc.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {


    @Autowired
    private IRoleService roleService;
    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.insert(role);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            roleService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除多个信息
    */
    @DeleteMapping
    public AjaxResult batchDelete(List<Serializable> ids){
        try {
                roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id)
    {
        try {
            Role role = roleService.selectById(id);
            return AjaxResult.success(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Role> list = roleService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }
    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody RoleQuery query)
    {
        try {
            PageInfo<Role> pageInfo = roleService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 授权
     *   当前段传到后端的参数不能与domain的参数不匹配时 这个时候domain中的参数不能随便更改
     *   可自定义一个实体类
     *   这个时候就应该自定义一个接受对象 这个对象叫做 DTO  (Data Transfer Object)
     *   后台一个接口需要返回多个domain的值给前端 也需要一个自定义对象 VO (view Object)
     *
     */

    @PostMapping("/roleAuth")
    public AjaxResult roleAuth(@RequestBody RoleDTO roleDTO)
    {
        try {
             roleService.roleAuth(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    @PostMapping("/roleAuth/{id}")
    public AjaxResult roleAuth(@PathVariable("id") String id)
    {
        try {
            List<Long> ids = roleService.queryAuthByRoleId(id);
            return AjaxResult.success(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

}
